package com.zhijiaoyun.service.impl;

import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.common.ResponseUtils;
import com.zhijiaoyun.dao.ICourseDao;
import com.zhijiaoyun.dao.ICourseHistoryDao;
import com.zhijiaoyun.dao.ILearnHistoryDao;
import com.zhijiaoyun.dao.ILessonDao;
import com.zhijiaoyun.model.CourseHistory;
import com.zhijiaoyun.model.LearnHistory;
import com.zhijiaoyun.model.Lesson;
import com.zhijiaoyun.model.dto.*;
import com.zhijiaoyun.service.ICourseService;
import com.zhijiaoyun.service.ILearnHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "learnHistoryService")
public class LearnHistoryServiceImpl implements ILearnHistoryService {

    @Resource
    ILearnHistoryDao learnHistoryDao;

    @Resource
    ICourseHistoryDao courseHistoryDao;

    @Resource
    ILessonDao lessonDao;

    @Resource
    ICourseDao courseDao;

    @Resource
    ICourseService courseService;

    @Override
    public PageList<LearnHistory> getLearnHistoryByParams(Integer pageIndex, String username, String coursename) {
        int count = learnHistoryDao.countByParams(username, coursename, null);
        PageData pageData = new PageData();
        List<LearnHistory> list = new ArrayList<>();
        PageList<LearnHistory> pageList = new PageList<>();
        if (count > 0) {
            int pageIndexInt = pageIndex == null ? 1 : pageIndex;
            pageData = PageData.createPage(count, pageIndexInt, DsConstant.PAGESIZE_DEFAULT);
            list = learnHistoryDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), username, coursename, null);
            pageList = new PageList<>(list, pageData);
        }
        return pageList;
    }

    @Override
    public List<LearnHistory> pageLearnHistoryByUserId(Integer pageIndex, Long userId) {
        if (pageIndex == null || pageIndex <= 0) {
            pageIndex = 1;
        }
        int beginIndex = (pageIndex - 1) * DsConstant.PAGESIZE_DEFAULT;
        return learnHistoryDao.pageLearnHistoryByUserId(beginIndex, DsConstant.PAGESIZE_DEFAULT, userId);
    }

    @Override
    public List<LearnHistory> getLearnHistoryByCourseIdUserId(Long courseId, Long userId) {
        return learnHistoryDao.selectLearnHistoryByCourseIdUserId(courseId, userId);
    }

    @Override
    public void addLearnHistory(LearnHistory learnHistory) {
        learnHistoryDao.insert(learnHistory);
    }

    @Override
    public LearnHistory getLearnHistoryByLessonIdUserId(Long lessonId, Long userId) {
        return learnHistoryDao.selectByLessonIdUserId(lessonId, userId);
    }

    @Override
    public PageList<LearnHistory> pageLearnHistoryByParams(Integer pageIndex, Long userId, Long courseId) {

        int count = learnHistoryDao.countByParams(null, null, userId);
        PageData pageData = new PageData();
        List<LearnHistory> list = new ArrayList<>();
        PageList<LearnHistory> pageList = new PageList<>();
        if (count > 0) {
            int pageIndexInt = pageIndex == null ? 1 : pageIndex;
            pageData = PageData.createPage(count, pageIndexInt, DsConstant.PAGESIZE_DEFAULT);
            list = learnHistoryDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), null, null, userId);
            pageList = new PageList<>(list, pageData);
        }
        return pageList;
    }

    @Override
    public List<LearnHistoryDto> getLearnHistoryDtoByParams(Long userId, Long courseId) {
        List<LearnHistoryDto> learnHistoryDtos = new ArrayList<>();
        List<Lesson> lessons = lessonDao.selectLessonByCourseId(courseId);
        if (!StringUtils.isEmptyList(lessons)) {
            // 获取该用户改课程 最新学习的那条数据
            LearnHistory learnHistoryL = learnHistoryDao.selectLastLearnHistory(courseId, userId);
            for (Lesson lesson : lessons) {
                LearnHistory learnHistory = learnHistoryDao.selectByLessonIdUserId(lesson.getLessonId(), userId);
                LearnHistoryDto learnHistoryDto = new LearnHistoryDto(lesson, learnHistory);

                if (learnHistory != null && learnHistoryL != null && (Objects.equals(learnHistory.getHistoryId(), learnHistoryL.getHistoryId()))) {
                    learnHistoryDto.setLastLearn(true);
                }
                learnHistoryDtos.add(learnHistoryDto);
            }
        }

        return learnHistoryDtos;
    }

    @Override
    public void modifyLearnHistory(LearnHistory learnHistory) {
        learnHistoryDao.updateByPrimaryKey(learnHistory);
    }

    @Override
    public PageList<CourseHistoryDto> pageCourseHistoryDtoByParams(Integer pageIndex, Long userId) {
        PageList<CourseHistoryDto> pageList = new PageList<>();
        return pageList;
    }

    @Override
    public List<CourseHistoryDto> getCourseHistoryDtoByParams(Integer pageIndex, Long userId) {
        List<CourseHistoryDto> courseHistoryDtos = new ArrayList<>();
        if (pageIndex == null || pageIndex <= 0) {
            pageIndex = 1;
        }
        int beginIndex = (pageIndex - 1) * DsConstant.PAGESIZE_DEFAULT;
        List<CourseHistory> list = courseHistoryDao.pageByParams(beginIndex, DsConstant.PAGESIZE_DEFAULT, userId);

        if (!StringUtils.isEmptyList(list)) {
            for (CourseHistory courseHistory : list) {
                CourseDto courseDto = courseService.getCourseDtoByCourseId(courseHistory.getCourseId());
                if (courseDto != null) {
                    CourseHistoryDto courseHistoryDto = new CourseHistoryDto(courseDto, courseHistory);
                    courseHistoryDtos.add(courseHistoryDto);
                }
            }
        }

        return courseHistoryDtos;
    }

    @Override
    public PageList<CourseHistoryDto> pageCourseHistoryDtoByParams(int pageIndex, int pageSize,
                                                                   long userId) {
        int count = courseHistoryDao.countByParams(userId);
        PageData pageData = new PageData();
        List<CourseHistoryDto> listD = new ArrayList<>();
        PageList<CourseHistoryDto> pageList = new PageList<>();
        if (count > 0) {
            pageData = PageData.createPage(count, pageIndex, pageSize);
            List<CourseHistory> list = courseHistoryDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), userId);
            if (!StringUtils.isEmptyList(list)) {
                for (CourseHistory courseHistory : list) {
                    CourseDto courseDto = courseService.getCourseDtoByCourseId(courseHistory.getCourseId());
                    if (courseDto != null) {
                        CourseHistoryDto courseHistoryDto = new CourseHistoryDto(courseDto, courseHistory);
                        listD.add(courseHistoryDto);
                    }
                }
            }
            pageList = new PageList<>(listD, pageData);
        }
        return pageList;
    }

    @Override
    public Map<String, Object> pageCourseHistoryDtoByUserId(int pageIndex, int pageSize, long userId) {
        PageList<CourseHistoryDto> courseHistories = pageCourseHistoryDtoByParams(pageIndex, pageSize, userId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", courseHistories.getList());
        map.put("page", courseHistories.getPageData());
        return ResponseUtils.responseSuccessMap(map);
    }

    @Override
    public Map<String, Object> listLearnHistoryDtoByCourseIdUserId(long userId, long courseId) {
        List<LearnHistoryDto> learnHistorys = getLearnHistoryDtoByParams(userId, courseId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", learnHistorys);
        return ResponseUtils.responseSuccessMap(map);
    }

}
